    use IO::Socket;
    use IO::Select;
    use Thread;
    # Create a socket to listen on.
    #
    print "enter svr.pl \n";
    system("title svr.pl");
    my $listener = 
      IO::Socket::INET->new( LocalPort => 8008, Listen => 5, Reuse => 1 );
    
    die "Can't create socket for listening: $!" unless $listener;
    print "Listening for connections on port 8008\n";
    
    my $readable = IO::Select->new;     # Create a new IO::Select object
    $readable->add($listener);          # Add the listener to it
    
    my $done = 0;
    while(!$done) {
    
        # Get a list of sockets that are ready to talk to us.
        #
	print "listening new client connect\n";
        my (@ready) = $readable->can_read(); 
        foreach my $s (@ready) {
            
            # Is it a new connection?
            #
            if($s == $listener) {
            
                # Accept the connection and add it to our readable list.
                #
                my $new_sock = $listener->accept;
		&client_thread($new_sock);            
            }
        }
    }

sub log{
	my($cmd) = @_;
	my $log;
	open LOG,">>svr.pl.txt";
	print LOG "$cmd\n";
	system($cmd);
	close LOG;
	return $log;
}

sub client_thread
{
	threads->create(\&thread_work,@_);
}
sub thread_work{
	my ($sock) = @_;
	my $t;
	$t = Thread->self;
	print "new thread\n";
	my $readable = IO::Select->new;     # Create a new IO::Select object
	$readable->add($sock);          # Add the listener to it
	my $done = 0;
	while($done == 0)
	{
	        print "waiting readable\n" if $debug;
	        my (@ready) = $readable->can_read(); #IO::Select->select($readable, undef, undef, undef);
	        print "can readable\n"; if $debug;
	        foreach my $s (@ready) {
        	        my $buf = <$s>;   # Try to read a line
                
                	# Was there anyone on the other end?
	                #
        	        if( defined $buf ) {
                	    print $buf if $debug;
	                    # If they said goodbye, close the socket. If not,
        	            # echo what they said to us.
                	    #
			    if($buf =~/^armtcc.exe(.*)/){
				&systemlog($buf,$s);
				print $s "svr:end\n";
			    }elsif ($buf =~/^download:(\S+)/) {
				$filename = $1;
				use updown;
				upload($s,$filename);
				print "end upload\n" if $debug;	
			    }elsif ($buf =~/^upload/) {
				use updown;
				download($s);
				print "end download\n" if $debug;
			    }elsif ($buf =~/^quit/) {
				$s->close();
				$done = 1;
				last ;
		            } else {
			    }
        		            
                        } else { # The client disconnected.                
                        	$readable->remove($s);
                    		$s->close;
                    		print STDERR "Client Connection closed\n";   
				$done = 1;
				last ;
			}
                }
	}
	$t->detach();
	print "Thread exit \n";
}

sub systemlog
{
	my($cmd,$log) = @_;
	$cmd =~ s/\n//;
	open R,"$cmd 2>&1 |";
	while(<R>)
	{
		print $log "$_";
	}	
	close(R);
}


print "exit svr.pl \n";